import 'package:flutter/material.dart';
import 'package:flutter_demo/pages/video/video_net_view.dart';
import 'package:video_player/video_player.dart';
import 'package:visibility_detector/visibility_detector.dart';


///****视频列表****
class VideoListPage extends StatefulWidget {
  const VideoListPage({super.key});

  @override
  State<VideoListPage> createState() => _VideoListPageState();
}

class _VideoListPageState extends State<VideoListPage> {
  final List<String> _list = [
    "https://www.w3school.com.cn/example/html5/mov_bbb.mp4",
    "https://www.w3schools.com/html/movie.mp4",
    "https://v-cdn.zjol.com.cn/276982.mp4",
    "https://v-cdn.zjol.com.cn/276984.mp4",
    "https://v-cdn.zjol.com.cn/276985.mp4",
    "https://v-cdn.zjol.com.cn/280443.mp4",
    "https://www.w3schools.com/html/movie.mp4",
    "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8",
    "https://www.w3school.com.cn/example/html5/mov_bbb.mp4",
    "https://www.w3schools.com/html/movie.mp4",
    "https://v-cdn.zjol.com.cn/276982.mp4",
    "https://v-cdn.zjol.com.cn/276984.mp4",
    "https://v-cdn.zjol.com.cn/276985.mp4",
    "https://v-cdn.zjol.com.cn/280443.mp4",
    "https://www.w3schools.com/html/movie.mp4",
    "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8",
    "https://www.w3school.com.cn/example/html5/mov_bbb.mp4",
    "https://www.w3schools.com/html/movie.mp4",
    "https://v-cdn.zjol.com.cn/276982.mp4",
    "https://v-cdn.zjol.com.cn/276984.mp4",
    "https://v-cdn.zjol.com.cn/276985.mp4",
    "https://v-cdn.zjol.com.cn/280443.mp4",
    "https://www.w3schools.com/html/movie.mp4",
    "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8",
    "https://www.w3school.com.cn/example/html5/mov_bbb.mp4",
    "https://www.w3schools.com/html/movie.mp4",
    "https://v-cdn.zjol.com.cn/276982.mp4",
    "https://v-cdn.zjol.com.cn/276984.mp4",
    "https://v-cdn.zjol.com.cn/276985.mp4",
    "https://v-cdn.zjol.com.cn/280443.mp4",
    "https://www.w3schools.com/html/movie.mp4",
    "http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"
  ];

  int currentIndex = 0;
  List<VideoPlayerController> controllers = [];
  VideoPlayerController? currentController;

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    for (var e in controllers) {
      e.dispose();
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _buildBody,
    );
  }

  VideoPlayerController initController(String path) {
    VideoPlayerController videoPlayerController =
        VideoPlayerController.networkUrl(Uri.parse(path));
    videoPlayerController.addListener(() {
      //视频播放
    });
    videoPlayerController.setLooping(true);
    videoPlayerController.initialize().then((value) {
      if (mounted) {
        setState(() {});
      }
    });

    return videoPlayerController;
  }

  ///
  get _buildBody => VisibilityDetector(
      key: Key("VideoListPage"),
      onVisibilityChanged: (visibilityInfo) {
        var visiblePercentage = visibilityInfo.visibleFraction * 100;
        print(visiblePercentage);
        if (visiblePercentage > 0) {
          //页面可见
          if (visiblePercentage == 100) {
            currentController?.play();
            //print("开始播放");
          }
        } else {
          //页面不可见
          if (currentController != null) {
            currentController?.pause();
          }
        }
      },
      child: Stack(children: [
        RefreshIndicator(
          onRefresh: () async {},
          child: PageView.builder(

              allowImplicitScrolling: true,
              itemCount: _list.length,
              physics: const BouncingScrollPhysics(),
              scrollDirection: Axis.vertical,
              onPageChanged: (page) async {
                if (currentController != null) {
                  await currentController?.pause();
                }
                currentController = controllers[page];
                await currentController?.play();
              },
              itemBuilder: (context, index) => buildItem(index)),
        )
      ]));

  buildItem(int index) {
    VideoPlayerController videoPlayerController;
    if (index >= controllers.length) {
      videoPlayerController = initController(_list[index]);
      controllers.add(videoPlayerController);
    } else {
      videoPlayerController = controllers[index];
    }
    return VideoNetView(
      videoPlayerController: videoPlayerController,
    );
  }
}
